package com.webapp.domain.entity;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import com.bridge.domain.AbstractVO;
import com.google.gson.Gson;
import com.google.gson.JsonObject;


@SuppressWarnings("serial")
@MappedSuperclass
public class BaseEntity implements Serializable {
	protected String name;
	/**
	 * 将VO转为JsonObject对象
	 * 使用自己的逻辑进行转换，当遇到对象为lazy加载时则跳过插入jsonobject，以免发生报错。
	 * @return com.google.gson.JsonObject
	 */
	public JsonObject toJsonObject(){
		JsonObject jsonObject=new JsonObject();
		Method[] methods = this.getClass().getDeclaredMethods();
		for (Method method : methods) {
			//get方法
			if(method.getName().indexOf("get")==0){
				try {
					String key=method.getName().substring(3,4).toLowerCase()+method.getName().substring(4);
					Field field=this.getClass().getDeclaredField(key);
					if(field.getGenericType().toString().indexOf("domain.entity")>=0||
							field.getGenericType().toString().indexOf("List")>=0){
					}else {
						Object value = method.invoke(this);
						if (value == null) {
							value = "";
						}
						jsonObject.addProperty(key, value.toString());
					}
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchFieldException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		//getName方法
		try {
			Method method = this.getClass().getSuperclass().getDeclaredMethod("getName");
			Object value = method.invoke(this);if (value == null) {
				value = "";
			}
			jsonObject.addProperty("name", value.toString());
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return jsonObject;
	}
	public static <T>T fromJsonObject(JsonObject jsonObject,Class<T> classOfT){
		if(!(AbstractVO.class.isAssignableFrom(classOfT))&&
				!JsonObject.class.isAssignableFrom(classOfT))return null;
		return new Gson().fromJson(jsonObject, classOfT);
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

}
